/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:flutter/material.dart';
import 'package:hadss_adaptive_layout/hadss_adaptive_layout.dart';
import 'package:provider/provider.dart';

import '../../main.dart';
import 'components/grid.dart';
import 'components/payComponet.dart';
import 'components/top.dart';

class MusicHomePage extends StatefulWidget {
  const MusicHomePage({super.key});

  @override
  State<MusicHomePage> createState() => _MusicHomePageState();
}

class _MusicHomePageState extends State<MusicHomePage>
    with WidgetsBindingObserver {
  double _currentWidth = 0;
  double _currentHeight = 0;
  late WidthBreakpoint screenType;
  String textFieldValue = '';
  num topCurrentIndex = 0;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    updateScreenSize();
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeMetrics() {
    updateScreenSize();
  }

  void updateScreenSize() {
    final Size size = WidgetsBinding.instance.window.physicalSize;
    final double ratio = WidgetsBinding.instance.window.devicePixelRatio;
    setState(() {
      _currentWidth = size.width / ratio;
      _currentHeight = size.height / ratio;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<RouteManager>(builder: (context, provider, child) {
      screenType = provider.widthBreakpoint;
      return _contentView(context);
    });
  }

  Widget _contentView(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Container(
            height: _currentHeight,
            decoration: const BoxDecoration(color: Colors.white),
            child: SingleChildScrollView(
              child: Container(
                color: const Color(0xFFFFFFFF),
                child: Column(
                  children: [
                    screenType == WidthBreakpoint.lg ||
                            screenType == WidthBreakpoint.xl
                        ? Container()
                        : Head(
                            screenType: screenType,
                            currentWidth: _currentWidth,
                            currentHeight: _currentHeight),
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        screenType == WidthBreakpoint.lg ||
                                screenType == WidthBreakpoint.xl
                            ? Head(
                                screenType: screenType,
                                currentWidth: _currentWidth,
                                currentHeight: _currentHeight)
                            : Container(),
                        Expanded(
                          child: Container(
                              margin: const EdgeInsets.only(top: 50),
                              child: GridWidget(
                                  screenType: screenType,
                                  currentWidth: _currentWidth)),
                        )
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
          Positioned(
              bottom: 28,
              child: PayComponent(
                  screenType: screenType, currentWidth: _currentWidth)),
        ],
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
